package com.mmy.controller.oa;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mmy.controller.Common;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.line.LineOrderServiceImpl;
import com.mmy.service.oa.impl.AccountService;

@Controller
public class AccountController {

	@Autowired
	AccountService accountService;
	@Autowired
	LineOrderServiceImpl lineOrderService;
	@Autowired
	Common common;

	private static Logger logger = LoggerFactory.getLogger(AccountController.class);
	
	/**
	 * 函数功能说明：跳转--会计审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人  liys
	 * @date 创建日期 2021.04.06
	 */
	@RequestMapping("/account")
	public String toAccountList(Model model,HttpServletRequest request) {
		
		String type = request.getParameter("type");
		model.addAttribute("type",type);
		String sort = request.getParameter("sort");
		String strState = request.getParameter("state");
		model.addAttribute("sort",sort);
		int iState = Integer.parseInt(strState);
		model.addAttribute("state",iState);
		if(iState == 4)return "OA/account/AccountOrderCancel";
		else return "OA/account/AccountOrderPaid";
	}
	
	/**
	 * 函数功能说明：跳转--近郊游会计审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人  liys
	 * @date 创建日期 2021.04.06
	 */
	@RequestMapping("/accountsurround")
	public String toSurroundList(Model model,HttpServletRequest request) {
		
		String strState = request.getParameter("state");
		int iState = Integer.parseInt(strState);
		model.addAttribute("state",iState);
		if(iState == 4)return "OA/account/AccountSurroundCancel";
		else return "OA/account/AccountSurroundPaid";
	}
	
	/**
	 * 函数功能说明：异步--获取审核列表内容
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人  liys
	 * @date 创建日期 2021.04.06
	 */
	@RequestMapping("/accountjson")
	@ResponseBody
	public String getAccountJson(HttpSession session,HttpServletRequest request,String sort,String state,String type,String accountState) {
		
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int index = (page - 1) * rows;
		int count = rows;
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		int iOrgID = tSysUser.getOrgID();
		String table = common.getViewName(sort);
		int iState = Integer.parseInt(state);
		List<Map<String,Object>> res = accountService.findAccountList(iOrgID, table,iState,type,index,count,accountState);
		return JSONObject.toJSONString(res);
	}
	/**
	 * 函数功能说明：异步--获取近郊游审核列表内容
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人  liys
	 * @date 创建日期 2021.04.06
	 */
	@RequestMapping("/accountsurroundjson")
	@ResponseBody
	public String getCashierJson(HttpSession session,HttpServletRequest request,String state,String accountState) {
		
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int index = (page - 1) * rows;
		int count = rows;
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		int iOrgID = tSysUser.getOrgID();
		int iState = Integer.parseInt(state);
		List<Map<String,Object>> res = accountService.findAccountSurround(iOrgID,iState,index,count,accountState);
		return JSONObject.toJSONString(res);
	}
	/**
	 * 函数功能说明：通过审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期 修改日期 2021.04.07
	 */
	@RequestMapping("/accountpass")
	@ResponseBody
	public String accountPass(HttpServletRequest request, HttpSession session, String checkList,String sort) {
		
		System.out.println("sort----" + sort);
		String table = common.getTableName(sort);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		int iOrgID = tSysUser.getUserID();
		String[] arrChecked = checkList.split(",");
		for (String str : arrChecked) {
			int iID = Integer.parseInt(str);
			accountService.accountPass(table, iID, iOrgID);
		}
		return null;
	}
	
	/**
	 * 函数功能说明：驳回审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期 修改日期 2021.04.07
	 */
	@RequestMapping("/accountfail")
	@ResponseBody
	public String accountFail(HttpServletRequest request, HttpSession session, String checkList,String sort) {
		
		String table = common.getTableName(sort);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		int iOrgID = tSysUser.getUserID();
		String[] arrChecked = checkList.split(",");
		for (String str : arrChecked) {
			int iID = Integer.parseInt(str);
			accountService.accountFail(table, iID, iOrgID);
		}
		return null;
	}
	
	/**
	 * 函数功能说明：近郊游审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期 修改日期 2021.04.07
	 */
	@RequestMapping("/accountsurroundcheck")
	@ResponseBody
	public String accountSurroundCheck(HttpServletRequest request, HttpSession session, String checkList,String accountState) {
		
		int iAccountState = Integer.parseInt(accountState);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		int iOrgID = tSysUser.getUserID();
		String[] arrChecked = checkList.split(",");
		for (String str : arrChecked) {
			int iID = Integer.parseInt(str);
			accountService.accountSurroundCheck(iID, iOrgID,iAccountState);
		}
		return null;
	}
}
